﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using iMapa.UI.ModelView.Main;
using iMapa.Backend;
using iMapa.UI.ModelView;
using iMapa.UI.ModelView.Common;
using iMapa.UI.ModelView.Main.Wizard;
using iMapa.UI.View.Common;

namespace iMapa.UI.View.Main.Wizard
{
    /// <summary>
    /// Interaction logic for Ws1ModelImageInput.xaml
    /// </summary>
    public partial class Ws1ModelImageInput : UserControl,IWizardStep
    {
        

        public Ws1ModelImageInput()
        {
            CameraInput.getInstance();
            InitializeComponent();
            
        }

        private void refresh_Click(object sender, RoutedEventArgs e)
        {
            ((NewIMapWizardViewModel)this.DataContext).RestartCameraCapture();
        }


        private void createSnapShot(object sender, RoutedEventArgs e)
        {
            CameraCaptureViewModel capturemodel = (CameraCaptureViewModel)((CameraCaptureView)this.FindName("capture")).DataContext;
            capturemodel.PauseCapture();
            ((NewIMapWizardViewModel)this.DataContext).Bitmap = Utils.ImgUtils.ToBitmapSource(capturemodel.CapturedImage); 
        }

        private void continueCapture(object sender, RoutedEventArgs e)
        {
            CameraCaptureViewModel capturemodel = (CameraCaptureViewModel)((CameraCaptureView)this.FindName("capture")).DataContext;
            capturemodel.ResumeCapture();
            ((NewIMapWizardViewModel)this.DataContext).Bitmap = null;
        }

        private void openFileDialog(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            dlg.FileName = ""; // Default file name
            dlg.Multiselect = false;
            dlg.DefaultExt = ".jpg"; // Default file extension
            dlg.Filter = "Image Files(*.BMP;*.JPG;*.GIF,*.PNG)|*.BMP;*.JPG;*.GIF,*.PNG|All files (*.*)|*.*"; // Filter files by extension

            // Show open file dialog box
            Nullable<bool> result = dlg.ShowDialog();

            // Process open file dialog box results
            if (result == true)
            {
                // Open document
                try
                {
                    BitmapImage bi = new BitmapImage(new Uri(dlg.FileName));
                    filename.Text = dlg.FileName;
                    ((NewIMapWizardViewModel)this.DataContext).Filename = dlg.FileName;
                    fileimg.Source = bi;
                }
                catch
                {
                    filename.Text = "Wrong Format !!!";
                    ((NewIMapWizardViewModel)this.DataContext).Filename = "";
                    fileimg.Source = null;
                }
                
                
            }
        }

        private void clip_refresh_Click(object sender, RoutedEventArgs e)
        {
            ((NewIMapWizardViewModel)this.DataContext).ReadClipboard();
        }

        #region IWizardStep Members

        public bool IsNextStepValid()
        {
            return ((NewIMapWizardViewModel)this.DataContext).ValidateStep();
        }

        public void SetError()
        {
            ((TextBlock)this.FindName("error")).Visibility = Visibility.Visible;
        }

        public void Finish()
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
